/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-27 下午1:46 Created by dwzhang
 */
package com.jade.modules.cms.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.cms.*;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

/**
 * JPA方式的实现接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-27 下午1:46
 */
//@Named ("module_cmsUserContentDao")
public class JpaUserContentDao implements UserContentDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    @Caching (evict = {@CacheEvict (value = "module.cms.comment.g", key = "#comment.id"),
            @CacheEvict (value = "module.cms.comment.q", key = "#comment.entityId + '[%GRP%]'")})
    public void addComment(Comment comment)
            throws CmsException
    {
        entityManager.persist(comment);
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.comment.g", key = "#comment.id"),
            @CacheEvict (value = "module.cms.comment.q", key = "#comment.entityId + '[%GRP%]'")})
    public void updateComment(Comment comment)
    {
        entityManager.merge(comment);
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.comment.g", key = "#comment.id"),
            @CacheEvict (value = "module.cms.comment.q", key = "#comment.entityId + '[%GRP%]'")})
    public void removeComment(Comment comment)
            throws CmsException
    {
        Comment old = entityManager.find(Comment.class, comment.getId());
        if (old != null) {
            entityManager.remove(old);
        }
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.comment.g", allEntries = true),
            @CacheEvict (value = "module.cms.comment.q", key = "#comment.entityId + '[%GRP%]'")})
    public int removeComment(long contentId)
            throws CmsException
    {
        String sql = "DELETE FROM com.jade.modules.cms.Comment  WHERE entityType=:etype AND entityId=:eid";
        Query query = entityManager.createQuery(sql);
        query.setParameter("etype", Comment.ETYPE_CONTENT);
        query.setParameter("eid", contentId);
        return query.executeUpdate();
    }

    @Cacheable (value = "module.cms.comment.g", key = "#id")
    public Comment getComment(long id)
    {
        return entityManager.find(Comment.class, id);
    }

    public PagingList<Comment> queryComment(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL_COMMENT, entityManager, criteria);
    }

    @Cacheable (value = "module.cms.comment.q", key = "contentId + '[%GRP%]' + #criteria.toString()")
    public PagingList<Comment> queryComment(long contentId, Criteria criteria)
    {
        criteria.add(Restrictions.eq("entityType", CmsUserEntity.ETYPE_CONTENT));
        criteria.add(Restrictions.eq("entityId", contentId));
        return queryComment(criteria);
    }

    protected static final String SQL_COMMENT = "SELECT comment FROM com.jade.modules.cms.Comment comment";

    public void addFavorite(Favorite favorite)
            throws CmsException
    {
        entityManager.persist(favorite);
    }

    public void removeFavorite(Favorite favorite)
            throws CmsException
    {
        Favorite old = entityManager.find(Favorite.class, favorite.getId());
        if (old != null) {
            entityManager.remove(old);
        }
    }

//    @Override
    public int removeFavorite(String userType, String userId, String etype, long eid)
            throws CmsException
    {
        String sql = null;
        if (eid > 0) {
            sql = "DELETE FROM Favorite WHERE userType=:ut AND userId=:ui AND entityType=:etype AND entityId=:eid";
        }
        else {
            sql = "DELETE FROM Favorite WHERE userType=:ut AND userId=:ui";
        }
        Query query = entityManager.createQuery(sql);
        query.setParameter("ut", userType);
        query.setParameter("ui", userId);
        if (eid > 0) {
            query.setParameter("etype", etype);
            query.setParameter("eid", eid);
        }
        return query.executeUpdate();
    }

    //    @Cacheable (value = "module.cms.favorite.gc", key = "#contentId")
//    public long getFavoriteCount(long contentId)
//    {
//        String sql = "SELECT COUNT(*) FROM com.jade.modules.cms.Favorite favorite WHERE contentId=:contentId";
//        Query query = entityManager.createQuery(sql);
//        query.setParameter("contentId", contentId);
//        return DataUtils.getLong(query.getSingleResult(), 0);
//    }

    public Favorite getFavorite(long id)
    {
        return entityManager.find(Favorite.class, id);
    }

    public List<Favorite> listFavorite(Criteria criteria)
    {
        return SqlUtils.sqlQueryWithoutCount(SQL_FAVORITE, entityManager, criteria);
    }

    public PagingList<Favorite> queryFavorite(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL_FAVORITE, entityManager, criteria);
    }

    protected static final String SQL_FAVORITE = "SELECT favorite FROM com.jade.modules.cms.Favorite favorite";
}
